package com.itheima.service;


import com.baomidou.mybatisplus.core.metadata.IPage;
import com.itheima.dto.OrderReturnDto;
import com.itheima.pojo.OmsOrder;
import com.itheima.pojo.OmsOrderReturnApply;
import com.itheima.vo.PageResultVo;
import org.apache.dubbo.config.annotation.DubboReference;
import org.springframework.stereotype.Service;

import java.util.ArrayList;

@Service
public class OmsOrderReturnApplyService {

    @DubboReference
    private OmsOrderReturnApplyApi omsOrderReturnApplyApi;

    @DubboReference
    private OmsOrderApi orderApi;

    /**
     * 批量删除退货申请
     *
     * @param ids
     * @return
     */
    public void deletes(ArrayList<Integer> ids) {
        omsOrderReturnApplyApi.deletes(ids);
    }


    /**
     * 获取退货申请详情
     *
     * @param id
     * @return
     */
    public OmsOrderReturnApply findById(Integer id) {
        OmsOrderReturnApply omsOrderReturnApply = omsOrderReturnApplyApi.findById(id);
        return omsOrderReturnApply;
    }

    /**
     * 修改退货申请状态
     *
     * @param id
     * @param omsOrderReturnApply
     * @return
     */
    public void updateReturn(Integer id, OmsOrderReturnApply omsOrderReturnApply) {
        OmsOrderReturnApply applyApiById = omsOrderReturnApplyApi.findById(id);
        Integer status = omsOrderReturnApply.getStatus();
        OmsOrder order = new OmsOrder();
        order.setId(applyApiById.getOrderId());
        if (status == 1) order.setStatus(2);
        else order.setStatus(3);
        omsOrderReturnApplyApi.updateReturn(id, omsOrderReturnApply);
        orderApi.updateById(order);
    }


    /**
     * 分页查询退货申请
     *
     * @param orderReturnDto
     * @return
     */
    public PageResultVo<OmsOrderReturnApply> list(OrderReturnDto orderReturnDto) {
        IPage<OmsOrderReturnApply> page = omsOrderReturnApplyApi.list(orderReturnDto);
        PageResultVo<OmsOrderReturnApply> pageResultVo = new PageResultVo<>();
        pageResultVo.setPageNum(orderReturnDto.getPageNum());
        pageResultVo.setPageSize(orderReturnDto.getPageSize());
        Long total = page.getTotal();
        pageResultVo.setTotal(total.intValue());
        Long pages = page.getPages();
        pageResultVo.setTotalPages(pages.intValue());
        pageResultVo.setList(page.getRecords());
        return pageResultVo;
    }

}
